<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
		<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	    <script type="text/javascript" src="tgd/Services/Google/Search.js"></script>
		<script type="text/javascript" src="tgd/Media/MediaLibrary.js"></script>
		<script type="text/javascript" src="tgd/Media/Library.js"></script>
		<script type="text/javascript" src="tgd/Media/Entry.js"></script>
		<title>Media Library</title>
		<script type="text/javascript">
			//<![CDATA[

var tgd = tgd || {};
tgd.Media = tgd.Media || {};
tgd.Media.GUI = tgd.Media.GUI || {};
tgd.Media.GUI.Modal = tgd.Media.GUI.Modal || {};
tgd.Media.GUI.Links = tgd.Media.GUI.Links || {};

var handledUPC = false;  //state variable.  used when current library is not set.

function init() {
    tgd.Media.GUI.Modal.Button = document.getElementById("btnModal");
    tgd.Media.GUI.Modal.Message = document.getElementById("panMessage");
    tgd.Media.GUI.Modal.Container = document.getElementById("panModal");
    tgd.Media.GUI.Links.Scan = document.getElementById("lnkScan");

    document.getElementById("cbFastScanning").checked = getFastScanMode();

    tgd.Media.GUI.MedLib = new tgd.Media.MediaLibrary(
        function(msg) {
            if (msg.Status === 1) {
                if (tgd.Media.GUI.MedLib.getLibraries().length === 0) {
                    alert("You must first create libraries.");
                } else if (tgd.Media.GUI.MedLib.getCurrentLibrary() === null) {
                    alert("The library to scan into has not been chosen.  Please choose a library to scan into.");
                    loadLibraries();
                } else {
                    loadLibraries();
                    handleUPC();
                }
            } else {
                alert(msg.Message);
            }
        }
    );
}

function handleUPC() {
    if (window.location.search != "") {
        var upc = window.location.search.substr(5);

        var newEntry = new tgd.Media.Entry({ UPC : upc });

        tgd.Media.GUI.MedLib.getCurrentLibrary().addEntry(newEntry,
            function(results) {
                handledUPC = true;
                showModal(results.Message);
                document.getElementById("btnModal").value = "Close";
                
                tgd.Media.GUI.Modal.Button.onclick = function(event) {
                    hideModal();
                }

                if (getFastScanMode()) {
                    //todo: give cancel option
                    var handle = setTimeout(
                        function() {
                            showModal("Scanning again in 1 second. SIMO");
                            tgd.Media.GUI.Modal.Button.value = "Cancel"
                            tgd.Media.GUI.Modal.Button.onclick = function() {
                                clearInterval(handle);
                                hideModal();
                            }

                            handle = setTimeout(
                                function() {
                                    //window.location.href = "http://zxing.appspot.com/scan?ret=http://oak.cats.ohiou.edu/~td176301/javascript/1.9/Biblio1.htm?upc={CODE}&SCAN_FORMATS=UPC_A&SCAN_FORMATS=EAN_13";
                                    //window.location.replace("http://zxing.appspot.com/scan?ret=http://oak.cats.ohiou.edu/~td176301/javascript/1.9/Biblio1.htm?upc={CODE}&SCAN_FORMATS=UPC_A&SCAN_FORMATS=EAN_13");
                                    hideModal();
                                    simulateScanClick();
                                },
                                1500
                            );
                        },
                        1500
                    );
                }
            }
        );
    }
}

/*
 * NOTE: possible that "current library" is null
 */
function loadLibraries() {
    var ddlLibraries = document.getElementById("ddlLibraries");
    var libs = tgd.Media.GUI.MedLib.getLibraries();
    var currentLib = tgd.Media.GUI.MedLib.getCurrentLibrary();

    for (var i = 0; i < libs.length; ++i) {
        var lib = libs[i];
        var o = document.createElement("option");
        o.label = lib.getName();
        o.value = lib.getID();
        o.selected = o.value == (currentLib === null ? null : tgd.Media.GUI.MedLib.getCurrentLibrary().getID());
        ddlLibraries.options.add(o);
    }
}

/*
 * TODO: For modals to work on Android it may be necessary to hide or disable dropdown lists.
 */

function hideModal() {
	tgd.Media.GUI.Modal.Container.style.display = "none";
}

function showModal(msg) {
	tgd.Media.GUI.Modal.Message.textContent = msg;
	tgd.Media.GUI.Modal.Container.style.display = "block";
}

function getFastScanMode() {
    if (window.localStorage.FastScanning === undefined) {
        window.localStorage.FastScanning = "false";
    }

    return window.localStorage.FastScanning === "true"
}

function setFastScanMode(on) {
    window.localStorage.FastScanning = (on ? "true" : "false");
}

/* window.location didn't work.  got this from dev.moz site.  read up to understand */
function simulateScanClick() {
    var evt = document.createEvent("MouseEvents");
    evt.initMouseEvent("click", true, true, window,0, 0, 0, 0, 0, false, false, false, false, 0, null);
    var canceled = !tgd.Media.GUI.Links.Scan.dispatchEvent(evt);
    if(canceled) {
        // A handler called preventDefault
        //alert("canceled");
    } else {
        // None of the handlers called preventDefault
        //alert("not canceled");
    }
}

			//]]>
		</script>

		<style type="text/css">
			body,html{margin:0;height:100%;width:100%;background:#1C1C1C;font-family:sans-serif;}

			#menu{color:#666;margin:5px 0 0 5px;}
			#menu a{text-decoration:none;padding:3px 10px;margin:0 0 5px;color:#666;}
			#menu a.current{background:#a4c639;color:#333;border:solid 1px #7EA600;}

			#content{background:#333;}
            .optionRow{color:#ccc;text-align:center;padding:10px 0;}
			h1{background:#a4c639;color:#333;padding:7px 0;margin:0;text-align:center;position:relative;font-size:1em;}
			h1 a,#btnModal{text-decoration:none;color:#333;padding:5px 15px;border:solid 1px #7EA600;background:#C1DE68;}
			select{background:#333;border:solid 1px #ccc;color:#ccc;}

            #panModal{display:none;}
			#panModal,#trans,#data{position:fixed;top:0;bottom:0;right:0;left:0;}
			#trans{background:#000;opacity:0.8;}
			#data{text-align:center;padding-top:1em;}
			#panMessage{margin:25px;color:#ccc;}
		</style>
	</head>
	<body id="body" onload="init();">
		<div id="menu">
			<a href="Biblio1.htm" class="current">Scan</a>
			<a href="Biblio2.htm">Uncatagorized</a>
			<a href="Biblio3.htm">My Library</a>
		</div>
		<div id="content">
		    <h1>
		        <a id="lnkScan" href="http://zxing.appspot.com/scan?ret=http://oak.cats.ohiou.edu/~td176301/javascript/1.9/Biblio1.htm?upc={CODE}&SCAN_FORMATS=UPC_A&SCAN_FORMATS=EAN_13">SCAN</a>
		    </h1>
            <div class="optionRow">
			    <select id="ddlLibraries" onchange="if (!tgd.Media.GUI.MedLib.setCurrentLibraryID(this.value)) { alert('Library does not exist.'); } else if (!handledUPC) { handleUPC(); }">
			        <option value="-1">Choose a Library</option>
			    </select>
		    </div>
		    <div class="optionRow">
		        <label for="cbFastScanning">Fast Scanning?</label>
		        <input id="cbFastScanning" type="checkbox" onchange="setFastScanMode(event.target.checked);" />
		    </div>
		</div>
		
		<div id="panModal">
			<div id="trans"></div>
			<div id="data">
			    <div id="panMessage"></div>
				<input type="button" id="btnModal" />
			</div>
		</div>
	</body>
</html>
